﻿Imports Capa_Datos
Public Class FrmGestionUsuarios

    Public Sub CargarCombo()
        With cboFiltro.Items
            .Clear()
            .Add("Usuario")
            .Add("Nombre")
            .Add("Apellido")

        End With
    End Sub


    Public Sub CargarSectores()
        Dim sectores As New List(Of Capa_Datos.sector)
        Dim db As New Capa_Datos.ContextoBAseDataContext
        With cboSector.Items
            .Clear()
            sectores = db.traer_sectores()

            cboSector.DataSource = db.traer_sectores()
            cboSector.DisplayMember = "txt_desc"
            cboSector.ValueMember = "id_sector"
            'For Each sec In sectores
            '    .Add(sec.txt_desc.ToString)
            '    cboSector.ValueMember
            'Next

        End With


    End Sub

    Public Sub reiniciar_controles()
        txtUsuario.Text = ""
        txtPassword.Text = ""
        txtNombres.Text = ""
        txtApellido.Text = ""
        txtDni.Text = ""
        txt_tel.Text = ""
        txtEmail.Text = ""
        btnAlta.Enabled = False
    End Sub

    Public Function uRecuperar_valores_texto() As Capa_Datos.usuarios_personas
        Dim usuario As New Capa_Datos.usuarios_personas

        With usuario
            .usuario = txtUsuario.Text
            .pass = txtPassword.Text
            .nombre = txtNombres.Text
            .apellido = txtApellido.Text
            .dni = txtDni.Text
            .tel = txt_tel.Text
            .mail = txtEmail.Text
            .id_sector = cboSector.SelectedValue
            .txt_desc = cboSector.Text
        End With

        Return usuario

    End Function

    Public Function asignarvalores_textos() As Capa_Datos.usuarios_personas

        Dim db As New Capa_Datos.ContextoBAseDataContext
        Dim usuarios As New Capa_Datos.usuarios_personas
        With dgvUsuarios
            If .SelectedRows.Count = 1 Then

                usuarios.usuario = .Item(0, .CurrentRow.Index).Value
                usuarios.pass = .Item(1, .CurrentRow.Index).Value
                usuarios.nombre = .Item(2, .CurrentRow.Index).Value
                usuarios.apellido = .Item(3, .CurrentRow.Index).Value()
                usuarios.dni = .Item(4, .CurrentRow.Index).Value()
                usuarios.tel = .Item(5, .CurrentRow.Index).Value
                usuarios.mail = .Item(6, .CurrentRow.Index).Value
                usuarios.id_sector = .Item(7, .CurrentRow.Index).Value
                usuarios.txt_desc = .Item(7, .CurrentRow.Index).Value

                With usuarios
                    txtUsuario.Text = .usuario
                    txtPassword.Text = .pass
                    txtNombres.Text = .nombre
                    txtApellido.Text = .apellido
                    txtDni.Text = .dni
                    txt_tel.Text = .tel
                    txtEmail.Text = .mail
                    cboSector.SelectedValue = usuarios.id_sector
                End With

            End If

            Return usuarios

        End With
    End Function

    Private Sub btnSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalir.Click
        Me.Close()
    End Sub

    Private Sub FrmGestionUsuarios_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CargarCombo()
        CargarSectores()
        cboFiltro.SelectedIndex = 0
        reiniciar_controles()
    End Sub

    Private Sub btnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        Dim contexto As New Capa_Datos.ContextoBAseDataContext
        Dim lista As New List(Of Capa_Datos.clsPersonas)

        dgvUsuarios.DataSource = contexto.buscarUsuarioPorFiltro(cboFiltro.Text.ToString, cboCriterios.Text.ToString, txtBusqueda.Text.Trim)
        'dgvUsuarios.DataSource = contexto.traer_usuarios()
        btnAlta.Enabled = False
    End Sub

    Private Sub cboFiltro_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboFiltro.SelectedIndexChanged

        If cboFiltro.SelectedIndex = 0 Then
            'CargarCriteriosNumericos(cboCriterios)
            CargarCriteriosTexto(cboCriterios)
            cboCriterios.SelectedIndex = 0
        End If

        If cboFiltro.SelectedIndex = 1 Or cboFiltro.SelectedIndex = 2 Then
            CargarCriteriosTexto(cboCriterios)
            cboCriterios.SelectedIndex = 1
        End If



    End Sub

    Private Sub cboCriterios_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCriterios.SelectedIndexChanged

    End Sub

    Private Sub txt_tel_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_tel.KeyPress
        Try
            e.Handled = validar.TELEFONO(e.KeyChar)
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_tel.TextChanged

    End Sub

    Private Sub Label10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label10.Click

    End Sub

    Private Sub dgvUsuarios_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvUsuarios.CellContentClick

        Dim row As DataGridViewSelectedRowCollection
        Dim db As New Capa_Datos.ContextoBAseDataContext
        Dim usuarios As New Capa_Datos.usuarios_personas
        With dgvUsuarios
            If .SelectedRows.Count = 1 Then
                row = .SelectedRows

                usuarios.usuario = .Item(0, .CurrentRow.Index).Value
                usuarios.pass = .Item(1, .CurrentRow.Index).Value
                usuarios.nombre = .Item(2, .CurrentRow.Index).Value
                usuarios.apellido = .Item(3, .CurrentRow.Index).Value()
                usuarios.dni = .Item(4, .CurrentRow.Index).Value()
                usuarios.tel = .Item(5, .CurrentRow.Index).Value
                usuarios.mail = .Item(6, .CurrentRow.Index).Value
                usuarios.id_sector = .Item(7, .CurrentRow.Index).Value
                usuarios.txt_desc = .Item(7, .CurrentRow.Index).Value

                With usuarios
                    txtUsuario.Text = .usuario
                    txtPassword.Text = .pass
                    txtNombres.Text = .nombre
                    txtApellido.Text = .apellido
                    txtDni.Text = .dni
                    txt_tel.Text = .tel
                    txtEmail.Text = .mail
                    cboSector.SelectedValue = usuarios.id_sector
                End With


            End If

        End With
        btnAlta.Enabled = False


    End Sub

    Private Sub btnAlta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAlta.Click
     

        If txtUsuario.Text = "" Then
            MsgBox("El campos que contienen (*) son obligatorios", MsgBoxStyle.OkOnly, "Advertencia")
            Exit Sub
        End If
        If txtPassword.Text = "" Then
            MsgBox("El campos que contienen (*) son obligatorios", MsgBoxStyle.OkOnly, "Advertencia")
            Exit Sub
        End If
        If txtNombres.Text = "" Then
            MsgBox("El campos que contienen (*) son obligatorios", MsgBoxStyle.OkOnly, "Advertencia")
            Exit Sub
        End If
        If txtApellido.Text = "" Then
            MsgBox("El campos que contienen (*) son obligatorios", MsgBoxStyle.OkOnly, "Advertencia")
            Exit Sub
        End If

        If txtDni.Text = "" Then
            MsgBox("El campos que contienen (*) son obligatorios", MsgBoxStyle.OkOnly, "Advertencia")
            Exit Sub
        End If

        If txt_tel.Text = "" Then
            txt_tel.Text = ""
        End If

        If txtEmail.Text = "" Then
            txtEmail.Text = ""
        End If

        If cboSector.Text = "" Then
            MsgBox("El campos que contienen (*) son obligatorios", MsgBoxStyle.OkOnly, "Advertencia")
            Exit Sub
        End If


        Dim db As New Capa_Datos.ContextoBAseDataContext
        Dim usuarioGral As Capa_Datos.usuarios_personas
        Dim tablaUsuario As New Capa_Datos.usuarios
        Dim tablaPersonas As New Capa_Datos.personas
        Try
            usuarioGral = uRecuperar_valores_texto()


            With tablaUsuario
                .usuario = usuarioGral.usuario
                .pass = usuarioGral.pass
                .estado = 1
            End With

            With tablaPersonas
                .usuario = usuarioGral.usuario
                .apellido = usuarioGral.apellido
                .nombre = usuarioGral.nombre
                .tel = usuarioGral.tel
                .mail = usuarioGral.mail
                .id_sector = usuarioGral.id_sector
                .dni = usuarioGral.dni
                .estado = 1

            End With



            db.usuarios.InsertOnSubmit(tablaUsuario)
            db.personas.InsertOnSubmit(tablaPersonas)

            db.SubmitChanges()
            MsgBox("El usuario fue agregado", MsgBoxStyle.OkOnly, "Alta Usuario")
            reiniciar_controles()
            btnBuscar_Click(sender, e)

        Catch ex As Exception
            MessageBox.Show(ex.ToString)

        End Try

    End Sub

    Private Sub dgvUsuarios_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvUsuarios.SelectionChanged
        Dim row As DataGridViewSelectedRowCollection
        Dim db As New Capa_Datos.ContextoBAseDataContext
        Dim usuarios As New Capa_Datos.usuarios_personas
        With dgvUsuarios
            If .SelectedRows.Count = 1 Then
                row = .SelectedRows

                usuarios.usuario = .Item(0, .CurrentRow.Index).Value
                usuarios.pass = .Item(1, .CurrentRow.Index).Value
                usuarios.nombre = .Item(2, .CurrentRow.Index).Value
                usuarios.apellido = .Item(3, .CurrentRow.Index).Value()
                usuarios.dni = .Item(4, .CurrentRow.Index).Value()
                usuarios.tel = .Item(5, .CurrentRow.Index).Value
                usuarios.mail = .Item(6, .CurrentRow.Index).Value
                usuarios.id_sector = .Item(7, .CurrentRow.Index).Value
                usuarios.txt_desc = .Item(7, .CurrentRow.Index).Value

                With usuarios
                    txtUsuario.Text = .usuario
                    txtPassword.Text = .pass
                    txtNombres.Text = .nombre
                    txtApellido.Text = .apellido
                    txtDni.Text = .dni
                    txt_tel.Text = .tel
                    txtEmail.Text = .mail
                    cboSector.SelectedValue = usuarios.id_sector
                End With


            End If

        End With
        btnAlta.Enabled = False
    End Sub

    Private Sub btnLimpiar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLimpiar.Click
        reiniciar_controles()
        btnAlta.Enabled = True
        cboSector.SelectedValue = 1
    End Sub

    Private Sub btnPermisos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPermisos.Click
        FrmReporte.Show()
    End Sub

    Private Sub btnModif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModif.Click


        If txtUsuario.Text = "" Then
            MsgBox("El campos que contienen (*) son obligatorios", MsgBoxStyle.OkOnly, "Advertencia")
            Exit Sub
        End If
        If txtPassword.Text = "" Then
            MsgBox("El campos que contienen (*) son obligatorios", MsgBoxStyle.OkOnly, "Advertencia")
            Exit Sub
        End If
        If txtNombres.Text = "" Then
            MsgBox("El campos que contienen (*) son obligatorios", MsgBoxStyle.OkOnly, "Advertencia")
            Exit Sub
        End If
        If txtApellido.Text = "" Then
            MsgBox("El campos que contienen (*) son obligatorios", MsgBoxStyle.OkOnly, "Advertencia")
            Exit Sub
        End If

        If txtDni.Text = "" Then
            MsgBox("El campos que contienen (*) son obligatorios", MsgBoxStyle.OkOnly, "Advertencia")
            Exit Sub
        End If

        If txt_tel.Text = "" Then
            txt_tel.Text = ""
        End If

        If txtEmail.Text = "" Then
            txtEmail.Text = ""
        End If

        If cboSector.Text = "" Then
            MsgBox("El campos que contienen (*) son obligatorios", MsgBoxStyle.OkOnly, "Advertencia")
            Exit Sub
        End If


        Dim db As New Capa_Datos.ContextoBAseDataContext
        Dim usuarioGral As Capa_Datos.usuarios_personas
        'Dim tablaUsuario As New Capa_Datos.usuarios
        'Dim tablaPersonas As New Capa_Datos.personas
        Dim tablaUsuario As New clsPersonas
        Dim tablaPersonas As New clsPersonas
        Try
            usuarioGral = uRecuperar_valores_texto()


            With tablaUsuario
                .usuario = usuarioGral.usuario
                .pass = usuarioGral.pass
                .estado = 1
            End With

            With tablaPersonas
                .USUARIO = usuarioGral.usuario
                .apellido = usuarioGral.apellido
                .NOMBRES = usuarioGral.nombre
                .tel = usuarioGral.tel
                .mail = usuarioGral.mail
                .id_sector = usuarioGral.id_sector
                .dni = usuarioGral.dni
                .estado = 1
                '
            End With

            tablaUsuario.Modificar_usuario(tablaUsuario)
            tablaPersonas.Modificar_persona(tablaPersonas)

            MsgBox("El usuario fue modificado", MsgBoxStyle.OkOnly, "Modificacion Usuario")
            reiniciar_controles()
            btnBuscar_Click(sender, e)


        Catch ex As Exception
            MessageBox.Show(ex.ToString)

        End Try

    End Sub

    Private Sub btnBaja_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBaja.Click

        If txtUsuario.Text = "" Then
            MsgBox("El campos que contienen (*) son obligatorios", MsgBoxStyle.OkOnly, "Advertencia")
            Exit Sub
        End If
        If txtPassword.Text = "" Then
            MsgBox("El campos que contienen (*) son obligatorios", MsgBoxStyle.OkOnly, "Advertencia")
            Exit Sub
        End If
        If txtNombres.Text = "" Then
            MsgBox("El campos que contienen (*) son obligatorios", MsgBoxStyle.OkOnly, "Advertencia")
            Exit Sub
        End If
        If txtApellido.Text = "" Then
            MsgBox("El campos que contienen (*) son obligatorios", MsgBoxStyle.OkOnly, "Advertencia")
            Exit Sub
        End If

        If txtDni.Text = "" Then
            MsgBox("El campos que contienen (*) son obligatorios", MsgBoxStyle.OkOnly, "Advertencia")
            Exit Sub
        End If

        If txt_tel.Text = "" Then
            txt_tel.Text = ""
        End If

        If txtEmail.Text = "" Then
            txtEmail.Text = ""
        End If

        If cboSector.Text = "" Then
            MsgBox("El campos que contienen (*) son obligatorios", MsgBoxStyle.OkOnly, "Advertencia")
            Exit Sub
        End If


        Dim db As New ContextoBAseDataContext
        Dim db2 As New ContextoBAseDataContext
        Dim usuarioGral As usuarios_personas
        Dim tablaUsuario As New usuarios
        Dim tablaPersonas As New personas
        Try
            usuarioGral = uRecuperar_valores_texto()


            With tablaUsuario
                .usuario = usuarioGral.usuario
                .pass = usuarioGral.pass
                .estado = 0
            End With

            With tablaPersonas

                .usuario = usuarioGral.usuario
                .nombre = usuarioGral.nombre
                .apellido = usuarioGral.apellido
                .dni = usuarioGral.dni
                .tel = usuarioGral.tel
                .mail = usuarioGral.mail
                .id_sector = usuarioGral.id_sector
                .estado = 0

            End With

            Dim per As New clsPersonas

            per.Eliminar_usuario(usuarioGral.usuario)

            per.Eliminar_Persona(usuarioGral.usuario)


            'db.personas.Attach(tablaPersonas)
            'db.personas.DeleteOnSubmit(tablaPersonas)
            'db.SubmitChanges()
            'db.usuarios.Attach(tablaUsuario)
            'db.usuarios.DeleteOnSubmit(tablaUsuario)

            'db.SubmitChanges()

            MsgBox("El usuario fue dado de baja", MsgBoxStyle.OkOnly, "Baja Usuario")
            reiniciar_controles()
            btnBuscar_Click(sender, e)


        Catch ex As Exception
            MessageBox.Show(ex.ToString)

        End Try


    End Sub

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub

    Private Sub txtUsuario_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtUsuario.KeyPress
        Try
            e.Handled = validar.ALFANUMERICO_SIN_SEPARACION(e.KeyChar)
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub txtUsuario_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtUsuario.TextChanged

    End Sub

    Private Sub txtPassword_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPassword.KeyPress
        Try
            e.Handled = validar.ALFANUMERICO_SIN_SEPARACION(e.KeyChar)
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub txtPassword_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPassword.TextChanged

    End Sub

    Private Sub txtNombres_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNombres.KeyPress
        Try
            e.Handled = validar.LETRAS_CON_SEPARACIONES(e.KeyChar)
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub txtNombres_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNombres.TextChanged

    End Sub

    Private Sub txtApellido_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtApellido.KeyPress
        Try
            e.Handled = validar.LETRAS_CON_SEPARACIONES(e.KeyChar)
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub txtApellido_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtApellido.TextChanged

    End Sub

    Private Sub txtDni_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDni.KeyDown

    End Sub

    Private Sub txtDni_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDni.KeyPress
        Try
            e.Handled = validar.NUMEROS__SIN_SEPARACION(e.KeyChar)
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub txtDni_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDni.TextChanged
        
    End Sub

    Private Sub txtEmail_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtEmail.KeyPress
        Try
            e.Handled = validar.Email(e.KeyChar)
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub txtEmail_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEmail.TextChanged

    End Sub
End Class

